//
//  SendMsgUtils.swift
//  xgwjinterconnection
//  发送信息工具类
//  Created by WangWeiShuo on 2025/9/1.
//
import Foundation

class SendMsgUtils{
    static let headData = Data("XCMG".utf8)  // 包头
    static let footerData = Data("$".utf8)   // 包尾
    
    //发送获取数据指令
    static func sendGetMessage(socket:SocketManager,type: UInt8){
        let msgData = Data([SendMessageType.TYPE_GET,type])
        let message = String(data: msgData, encoding: .utf8)
//        print("msgData = \(message)")
        print("获取: \(String(describing: message))")
        socket.sendMessageData(addHeaderAndFooter(data: msgData, header: headData, footer: footerData))
    }
    
    //拼接数据
    private static func addHeaderAndFooter(data: Data, header: Data, footer: Data) -> Data {
        // 创建一个新的Data，并依次拼接header、data、footer
        var newData = Data()
        newData.append(header)   // 拼接头部
        newData.append(data)     // 拼接原始数据
        newData.append(footer)   // 拼接尾部
        return newData
    }
    
    //发送控制指令
    static func sendControlMessage(socket:SocketManager,type: UInt8,value: UInt8){
        let msgData = Data([SendMessageType.TYPE_CONTROL,type,value])
        let message = String(data: msgData, encoding: .utf8)
        print("控制命令: \(String(describing: message))")
        socket.sendMessageData(addHeaderAndFooter(data: msgData, header: headData, footer: footerData))
    }

}
